using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
namespace DAL
{
    public class Votes : DAL.DataManager<Vote>
    {
        private static Votes Instance = new Votes();
        static Votes() { }

        public override string TableName
        {
            get { return @"fstdt_votes"; }
        }

        protected override void CopyToItem(DataRow Row, Vote Item)
        {
            Item.Quote = Quotes.Item(Convert.ToInt64(Row["QuoteID"]));
            Item.IPAddress = Convert.ToString(Row["IPAddress"]);
        }
        protected override void CopyFromItem(DataManagerQuery query, Vote Item)
        {
            query.AddParam("QuoteID", Item.Quote.ID);
            query.AddParam("IPAddress", Item.IPAddress);
        }

        public static Vote Item(Int64 id)
        {
            return Instance.GetItem(id);
        }

        public static List<Vote> All()
        {
            return Instance.GetItems();
        }

        public static void Add(Vote Item)
        {
            Instance.Insert(Item);
        }

        public static void Save(Vote Item)
        {
            Instance.Update(Item);
        }

        public static bool HasUserVoted(Quote quote, string IPAddress)
        {
            DataManagerQuery query = new DataManagerQuery();
            query.AddParam("QuoteID", quote.ID);
            query.AddParam("IPAddress", IPAddress);
            return Instance.GetCount(query) > 0;
        }
    }

    public class Vote : DAL.DataManagerRow
    {
        private Quote _Quote;
        public Quote Quote
        {
            get
            {
                return _Quote;
            }
            set
            {
                _Quote = value;
            }
        }


        private string _IPAddress;
        public string IPAddress
        {
            get
            {
                return _IPAddress;
            }
            set
            {
                _IPAddress = value;
            }
        }
    }
}
